package com.example.spring_java_oj.service.problem;

import com.example.spring_java_oj.Util.FileUtil;
import com.example.spring_java_oj.mapper.ProblemRedis;
import com.example.spring_java_oj.model.Problem;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.*;

/*
    从redis中获取题目
 */
@Service
public class ProblemRedisService {
    @Autowired
    private ProblemRedis problemRedis;

    private static ObjectMapper objectMapper = new ObjectMapper();

    public List<Problem> getList() throws JsonProcessingException {
        List<Object> list = problemRedis.getProblems();
        List<Problem> problems = new ArrayList<>();
        if (list == null || list.size() == 0){
            return null;
        }
        int size = list.size();
        for(int i = 0; i < size; i++){
            String problemStr = (String) list.get(i);
            Problem problem = objectMapper.readValue(problemStr, Problem.class);
            problems.add(problem);
        }
        return problems;
    }

    public Problem getProblem(Integer id, HttpServletRequest request) throws JsonProcessingException {
        String problemJson = problemRedis.getProblem(id);
        Problem problem = null;
        if (problemJson != null){
            problem = objectMapper.readValue(problemJson, Problem.class);
        }
//        HttpSession session = request.getSession();
//        String path ="./tmp/" + session.getAttribute("username") + id + "/";
//        File workDir = new File(path);
//        if(!workDir.exists()){
//            workDir.mkdirs();
//        }
//        String temPath = path + "finalCodeFile.txt";
//        File file = new File(temPath);
//        if(!file.exists()){
//            return problem;
//        }
//        String tem = FileUtil.readFile(temPath);
//        problem.setTemplateCode(tem);
        return problem;
    }

    public Problem getProblemStart(Integer id) throws JsonProcessingException {
        String problemJson = problemRedis.getProblem(id);
        Problem problem = null;
        if (problemJson != null){
            problem = objectMapper.readValue(problemJson, Problem.class);
        }
        return problem;
    }

    public Integer updateAndInsertProblem(Problem problem) throws JsonProcessingException {
        String json = objectMapper.writeValueAsString(problem);
        return problemRedis.insertAndUpdate(json, problem.getId());
    }

    public Integer insertProblems(List<Problem> list) throws JsonProcessingException {
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++){
            Problem problem = list.get(i);
            String json = objectMapper.writeValueAsString(problem);
            map.put(Integer.toString(problem.getId()), json);
        }
        return problemRedis.insertProblems(map);
    }

    public Integer deleteProblem(Integer id){
        return problemRedis.delete(id);
    }

}
